<?php

namespace App\Http\Controllers;

use Illuminate\Http\JsonResponse;
use Illuminate\Http\Response;

abstract class Controller
{
    /**
     * 成功响应
     *
     * @param mixed $data 响应数据
     * @param string $message 响应消息
     * @param int $code HTTP状态码
     * @return JsonResponse
     */
    protected function success($data = null, string $message = '操作成功', int $code = Response::HTTP_OK): JsonResponse
    {
        $response = [
            'success' => true,
            'code' => $code,
            'message' => $message,
        ];

        if ($data !== null) {
            $response['data'] = $data;
        }

        return response()->json($response, $code);
    }

    /**
     * 失败响应
     *
     * @param string $message 错误消息
     * @param mixed $errors 错误详情
     * @param int $code HTTP状态码
     * @return JsonResponse
     */
    protected function fail(string $message = '操作失败', $errors = null, int $code = Response::HTTP_BAD_REQUEST): JsonResponse
    {
        $response = [
            'success' => false,
            'code' => $code,
            'message' => $message,
        ];

        if ($errors !== null) {
            $response['errors'] = $errors;
        }

        return response()->json($response, $code);
    }

    /**
     * 错误响应（服务器错误）
     *
     * @param string $message 错误消息
     * @param mixed $errors 错误详情
     * @param int $code HTTP状态码
     * @return JsonResponse
     */
    protected function error(string $message = '服务器内部错误', $errors = null, int $code = Response::HTTP_INTERNAL_SERVER_ERROR): JsonResponse
    {
        $response = [
            'success' => false,
            'code' => $code,
            'message' => $message,
        ];

        if ($errors !== null) {
            $response['errors'] = $errors;
        }

        return response()->json($response, $code);
    }

    /**
     * 未授权响应
     *
     * @param string $message 错误消息
     * @return JsonResponse
     */
    protected function unauthorized(string $message = '未授权访问'): JsonResponse
    {
        return $this->fail($message, null, Response::HTTP_UNAUTHORIZED);
    }

    /**
     * 禁止访问响应
     *
     * @param string $message 错误消息
     * @return JsonResponse
     */
    protected function forbidden(string $message = '禁止访问'): JsonResponse
    {
        return $this->fail($message, null, Response::HTTP_FORBIDDEN);
    }

    /**
     * 未找到响应
     *
     * @param string $message 错误消息
     * @return JsonResponse
     */
    protected function notFound(string $message = '资源未找到'): JsonResponse
    {
        return $this->fail($message, null, Response::HTTP_NOT_FOUND);
    }

    /**
     * 验证失败响应
     *
     * @param mixed $errors 验证错误
     * @param string $message 错误消息
     * @return JsonResponse
     */
    protected function validationError($errors, string $message = '验证失败'): JsonResponse
    {
        return $this->fail($message, $errors, Response::HTTP_UNPROCESSABLE_ENTITY);
    }

    /**
     * 创建成功响应
     *
     * @param mixed $data 响应数据
     * @param string $message 响应消息
     * @return JsonResponse
     */
    protected function created($data = null, string $message = '创建成功'): JsonResponse
    {
        return $this->success($data, $message, Response::HTTP_CREATED);
    }

    /**
     * 更新成功响应
     *
     * @param mixed $data 响应数据
     * @param string $message 响应消息
     * @return JsonResponse
     */
    protected function updated($data = null, string $message = '更新成功'): JsonResponse
    {
        return $this->success($data, $message, Response::HTTP_OK);
    }

    /**
     * 删除成功响应
     *
     * @param string $message 响应消息
     * @return JsonResponse
     */
    protected function deleted(string $message = '删除成功'): JsonResponse
    {
        return $this->success(null, $message, Response::HTTP_OK);
    }

    /**
     * 分页响应
     *
     * @param mixed $data 分页数据
     * @param string $message 响应消息
     * @return JsonResponse
     */
    protected function paginated($data, string $message = '获取成功'): JsonResponse
    {
        return $this->success($data, $message);
    }

    /**
     * 无内容响应
     *
     * @param string $message 响应消息
     * @return JsonResponse
     */
    protected function noContent(string $message = '操作成功'): JsonResponse
    {
        return $this->success(null, $message, Response::HTTP_NO_CONTENT);
    }

    /**
     * 自定义响应
     *
     * @param bool $success 是否成功
     * @param string $message 响应消息
     * @param mixed $data 响应数据
     * @param mixed $errors 错误信息
     * @param int $code HTTP状态码
     * @return JsonResponse
     */
    protected function response(bool $success, string $message, $data = null, $errors = null, int $code = Response::HTTP_OK): JsonResponse
    {
        $response = [
            'success' => $success,
            'code' => $code,
            'message' => $message,
        ];

        if ($data !== null) {
            $response['data'] = $data;
        }

        if ($errors !== null) {
            $response['errors'] = $errors;
        }

        return response()->json($response, $code);
    }
}
